<!--
  * 一体化平台重点关爱人群
  *
  * @Author:    cfsjiazy
  * @Date:      2025-09-12 11:50:48
  * @Copyright   
-->
<template>
  <a-modal
      :title="form.id ? '编辑' : '添加'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >
        <a-form-item label="性别，字典：SEX"  name="gender">
          <a-input style="width: 100%" v-model:value="form.gender" placeholder="性别，字典：SEX" />
        </a-form-item>
        <a-form-item label="年龄"  name="age">
          <a-input-number style="width: 100%" v-model:value="form.age" placeholder="年龄" />
        </a-form-item>
        <a-form-item label="出生日期"  name="birthDate">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.birthDate" style="width: 100%" placeholder="出生日期" />
        </a-form-item>
        <a-form-item label="是否独居，字典：YN"  name="isAlone">
          <a-input style="width: 100%" v-model:value="form.isAlone" placeholder="是否独居，字典：YN" />
        </a-form-item>
        <a-form-item label="健康状态，字典：XF_PERSON_HEALH_STATUS"  name="healthStatus">
          <a-input style="width: 100%" v-model:value="form.healthStatus" placeholder="健康状态，字典：XF_PERSON_HEALH_STATUS" />
        </a-form-item>
        <a-form-item label="是否有拾荒习惯，字典：YN"  name="habit">
          <a-input style="width: 100%" v-model:value="form.habit" placeholder="是否有拾荒习惯，字典：YN" />
        </a-form-item>
        <a-form-item label="备注"  name="note">
          <a-textarea style="width: 100%" v-model:value="form.note" placeholder="备注" />
        </a-form-item>
        <a-form-item label="创建时间"  name="createTime">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.createTime" style="width: 100%" placeholder="创建时间" />
        </a-form-item>
        <a-form-item label="更新时间"  name="updateTime">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.updateTime" style="width: 100%" placeholder="更新时间" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { cfs119CarePersonApi } from '/@/api/yitihua/cfs119-care-person/cfs119-care-person-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      gender: undefined, //性别，字典：SEX
      age: undefined, //年龄
      birthDate: undefined, //出生日期
      isAlone: undefined, //是否独居，字典：YN
      healthStatus: undefined, //健康状态，字典：XF_PERSON_HEALH_STATUS
      habit: undefined, //是否有拾荒习惯，字典：YN
      note: undefined, //备注
      createTime: undefined, //创建时间
      updateTime: undefined, //更新时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
      gender: [{ required: true, message: '性别，字典：SEX 必填' }],
      age: [{ required: true, message: '年龄 必填' }],
      birthDate: [{ required: true, message: '出生日期 必填' }],
      isAlone: [{ required: true, message: '是否独居，字典：YN 必填' }],
      healthStatus: [{ required: true, message: '健康状态，字典：XF_PERSON_HEALH_STATUS 必填' }],
      habit: [{ required: true, message: '是否有拾荒习惯，字典：YN 必填' }],
      note: [{ required: true, message: '备注 必填' }],
      createTime: [{ required: true, message: '创建时间 必填' }],
      updateTime: [{ required: true, message: '更新时间 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await cfs119CarePersonApi.update(form);
      } else {
        await cfs119CarePersonApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
